﻿// -----------------------------------------------------------------------
// <copyright file="ImageController.cs" company="EPAM Systems">
////Copyright (c) EPAM Systems, 2012. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace MBOP.Website.Controllers
{
	#region Usings

	using System.Collections.Generic;
	using System.Web.Mvc;

	using MBOP.Core.Entities.Common;
	using MBOP.Core.Interfaces.Services;
	using MBOP.Website.CustomAttributes;
	using MBOP.Website.CustomResults;
	using MBOP.Website.Infrastructure.Mappers;
	using MBOP.Website.ViewModels.Common;

	#endregion

	/// <summary>
	/// Provides methods that respond to HTTP request that are made to image operations.
	/// </summary>
	public class ImageController : Controller
	{
		#region Constants

		/// <summary>
		/// Represents the mime type for text and html.
		/// </summary>
		private const string MIME_TEXT_HTML = "text/html";

		#endregion

		#region Private Fields

		/// <summary>
		/// Represents image service.
		/// </summary>
		private IImageService _imageService;

		/// <summary>
		/// Represents image mapper.
		/// </summary>
		private ImageMapper _imageMapper;

		#endregion

		#region Constructors

		/// <summary>
		/// Initializes a new instance of the <see cref="ImageController"/> class.
		/// </summary>
		/// <param name="imageService">The <see cref="IImageService"/> implementer</param>
		public ImageController(IImageService imageService)
		{
			this._imageService = imageService;
			this._imageMapper = new ImageMapper();
		}

		#endregion

		#region Actions

		/// <summary>
		/// Processes the add image request.
		/// </summary>
		/// <param name="imageViewModel">The image's view model.</param>
		/// <returns>The image id.</returns>
		[HttpPost]
		[AuthorizeAdmin]
		public JsonResult AddImage(ImageViewModel imageViewModel)
		{
			JsonResult result = null;

			this.TryValidateModel(imageViewModel);

			if (ModelState.IsValid)
			{
				Image image = this._imageMapper.MapImage(imageViewModel);

				string imageID = this._imageService.AddImage(image);

				string imageUrl = string.Empty;

				if(!string.IsNullOrEmpty(image.Uri))
				{
					imageUrl = image.Uri;
				}
				else if(image.Data!=null)
				{
					imageUrl = Url.Action("GetImage", new {id = imageID});
				}

				result = this.Json(new { ImageUrl = imageUrl, ImageID=imageID }, MIME_TEXT_HTML);
			}
			else
			{
				List<string> errors = new List<string>();

				foreach (var item in this.ModelState)
				{
					foreach (var error in item.Value.Errors)
					{
						errors.Add(error.ErrorMessage);
					}
				}

				result = this.Json(new { Errors = errors }, MIME_TEXT_HTML);
			}

			return result;
		}

		/// <summary>
		/// Processes the get image request
		/// </summary>
		/// <param name="id">The image id</param>
		/// <returns>The Image result, which encapsulates information of the result processing.</returns>
		public ImageResult GetImage(string id)
		{
			Image image = this._imageService.GetImage(id);

			return new ImageResult(image);
		}

		#endregion
	}
}
